public class Location {
    private String city;
    private double latitude;
    private double longitude;
    private String locationSource; // "manual", "ip", "gps"
    private boolean hasCoordinates;

    // 原有构造函数保持兼容性
    public Location(String city) {
        this.city = city;
        this.hasCoordinates = false;
        this.locationSource = "manual";
    }
    
    // 新增支持坐标的构造函数
    public Location(String city, double latitude, double longitude) {
        this.city = city;
        this.latitude = latitude;
        this.longitude = longitude;
        this.hasCoordinates = true;
        this.locationSource = "unknown";
    }
    
    // 完整构造函数
    public Location(String city, double latitude, double longitude, String locationSource) {
        this.city = city;
        this.latitude = latitude;
        this.longitude = longitude;
        this.hasCoordinates = true;
        this.locationSource = locationSource != null ? locationSource : "unknown";
    }

    public String getCity() {
        return city;
    }
    
    public void setCity(String city) {
        this.city = city;
    }
    
    public double getLatitude() {
        return latitude;
    }
    
    public void setLatitude(double latitude) {
        this.latitude = latitude;
        this.hasCoordinates = true;
    }
    
    public double getLongitude() {
        return longitude;
    }
    
    public void setLongitude(double longitude) {
        this.longitude = longitude;
        this.hasCoordinates = true;
    }
    
    public String getLocationSource() {
        return locationSource;
    }
    
    public void setLocationSource(String locationSource) {
        this.locationSource = locationSource;
    }
    
    public boolean hasCoordinates() {
        return hasCoordinates;
    }
    
    /**
     * 验证坐标是否有效
     */
    public boolean hasValidCoordinates() {
        return hasCoordinates && 
               latitude >= -90 && latitude <= 90 &&
               longitude >= -180 && longitude <= 180;
    }
    
    /**
     * 是否为自动获取的位置
     */
    public boolean isAutoDetected() {
        return "ip".equals(locationSource) || "gps".equals(locationSource);
    }
    
    @Override
    public String toString() {
        if (hasCoordinates) {
            return String.format("%s (%.4f, %.4f) [%s]", city, latitude, longitude, locationSource);
        } else {
            return String.format("%s [%s]", city, locationSource);
        }
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        
        Location location = (Location) obj;
        
        if (city != null ? !city.equals(location.city) : location.city != null) return false;
        if (hasCoordinates && location.hasCoordinates) {
            return Math.abs(latitude - location.latitude) < 0.001 && 
                   Math.abs(longitude - location.longitude) < 0.001;
        }
        return true;
    }
    
    @Override
    public int hashCode() {
        int result = city != null ? city.hashCode() : 0;
        if (hasCoordinates) {
            result = 31 * result + Double.hashCode(latitude);
            result = 31 * result + Double.hashCode(longitude);
        }
        return result;
    }
}
